<?php
/**
 * Created by PhpStorm.
 * User: wangjiangtao
 * Date: 2022/6/1
 * Time: 19:07
 */

namespace App\Modules\LuckDraw\Controllers;


use App\Http\Model\Gift;
use App\Http\Model\Hint;
use App\Lib\Response;
use App\Modules\LuckDraw\Services\AdminService;
use Illuminate\Http\Request;

class AdminController
{

    protected $service = null;
    protected $giftService;
    public function __construct()
    {
        $this->service = new AdminService();
        $this->giftService = new Gift();
    }

    public function addManualAward(Request $request){
        $luckId = intval($request->input('luck_id', 0));
        $userId = intval($request->input('user_id', 0));
        $awardId = intval($request->input('award_id', 0));
        $type = intval($request->input('type', 1));
        //不是数字，或者小于等于0 返回参数错误
        if (!is_numeric($luckId) || $luckId <= 0
            || !is_numeric($userId) || $userId <= 0
            || !is_numeric($awardId) || $awardId <= 0
            || !is_numeric($type) || $type <= 0) {
            return Response::json(Hint::PARAM_ERROR);
        }
        $price = 0;
        if ($type = 1){
            $awardData = $this->giftService->get_gift($awardId);
            $price = $awardData['price']??0;
        }
        $res = $this->service->addManualAward($luckId,$userId,$awardId,$type,$price);
        if($res){
            return Response::json(Hint::SUCCESS);
        }
        return Response::json(Hint::ERROR);
    }

    public function delManualAward(Request $request){
        $id = intval($request->input('id', 0));
        if (!is_numeric($id) || $id <= 0){
            return Response::json(Hint::PARAM_ERROR);
        }

        $res = $this->service->delManualAward($id);
        if($res){
            return Response::json(Hint::SUCCESS);
        }
        return Response::json(Hint::ERROR);
    }

    public function addActivityPool (Request $request){
        $desc = $request->input('desc');
        $luckId = $request->input('luck_id');
        if (!$desc || empty($luckId)){
            return Response::json(Hint::PARAM_ERROR);

        }
        $res = $this->service->addActivityPool($desc,$luckId);
        if($res){
            return Response::json(Hint::SUCCESS);
        }
        return Response::json(Hint::ERROR);

    }

    public function delActivityPool (Request $request){
        $id = $request->input('id');
        if (!$id){
            return Response::json(Hint::PARAM_ERROR);
        }

        $res = $this->service->delActivityPool($id);
        if($res){
            return Response::json(Hint::SUCCESS);
        }
        return Response::json(Hint::ERROR);
    }

    public function setActivityPool (Request $request){
        $pools = $request->input('pools');
        if (empty($pools)) {
            return Response::json(Hint::PARAM_ERROR);
        }
        $res = $this->service->updateLuckDrawPools($pools);
        if($res){
            return Response::json(Hint::SUCCESS);
        }
    }

    public function upActivityPool (Request $request){
        $id = $request->input('id');
        $desc = $request->input('desc');
        $luckId = $request->input('luck_id');
        $is_open = $request->input('is_open');

        if (!$id || !$desc || empty($luckId)){
            return Response::json(Hint::PARAM_ERROR);
        }
        $res = $this->service->upActivityPool($id, $desc, $luckId, $is_open);
        if($res){
            return Response::json(Hint::SUCCESS);
        }
        return Response::json(Hint::ERROR);
    }

    public function editLuckDraw (Request $request){
        $params = $request->input();
        if (empty($params)) {
            return Response::json(Hint::PARAM_ERROR);
        }

        $res = $this->service->editLuckDraw($params);
        if($res && $res['code']==Hint::SUCCESS){
            return Response::json(Hint::SUCCESS);
        }
        return Response::json(Hint::ERROR);
    }
}
